package org.new9uy.tetris.view;

import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

import org.new9uy.tetris.GameConstants;
import org.new9uy.tetris.GameData;
import org.new9uy.tetris.Settings;
import org.new9uy.tetris.controller.GUIController;
import org.new9uy.tetris.controller.State;

@SuppressWarnings("serial")
public class SettingsDialog
        extends JDialog
{

    private String name = Settings.PLAYER_NAME;
    private int drop = Settings.CONTROLLER_DROP;
    private int left = Settings.CONTROLLER_LEFT;
    private int right = Settings.CONTROLLER_RIGHT;
    private int speed = Settings.CONTROLLER_SPEEDUP;
    private int rotate = Settings.CONTROLLER_ROTATE;
    private String select;
    private int keyOfSelect;
    private JButton butDefault = new JButton("Default");
    private final JTextField nameField = new JTextField(name);
    private final JTextField dropField = new JTextField(KeyEvent.getKeyText(drop));
    private final JTextField leftField = new JTextField(KeyEvent.getKeyText(left));
    private final JTextField rightField = new JTextField(KeyEvent.getKeyText(right));
    private final JTextField speedField = new JTextField(KeyEvent.getKeyText(speed));
    private final JTextField rotateField = new JTextField(KeyEvent.getKeyText(rotate));
    private JTextField selectedControl = null;
    private KeySettingsController keylist = new KeySettingsController();

    public SettingsDialog(JFrame frame)
    {
        super(frame);
        buildObjects();
    }

    private void buildObjects()
    {

        JPanel p = new JPanel(new GridLayout(9, 2));

        p.add(new JLabel("Your name : "));
        p.add(nameField);

        p.add(new JLabel("Controls"));
        p.add(new JLabel());/////

        p.add(new JLabel("Drop"));
        p.add(dropField);
        dropField.setEditable(false);

        p.add(new JLabel("Left"));
        p.add(leftField);
        leftField.setEditable(false);

        p.add(new JLabel("Right"));
        p.add(rightField);
        rightField.setEditable(false);

        p.add(new JLabel("Speed Up"));
        p.add(speedField);
        speedField.setEditable(false);

        p.add(new JLabel("Rotate"));
        p.add(rotateField);
        rotateField.setEditable(false);

        p.add(new JLabel(""));
        butDefault.addActionListener(new ActionListener()
        {
            @Override
            public void actionPerformed(ActionEvent e)
            {
                defaultSettings();
            }
        });

        JButton ok = new JButton("Save Changes");
        ok.addActionListener(new ActionListener()
        {
            @Override
            public void actionPerformed(ActionEvent e)
            {
                ok();
            }
        });
        JButton cancel = new JButton("Cansel");
        cancel.addActionListener(new ActionListener()
        {
            @Override
            public void actionPerformed(ActionEvent e)
            {
                cancel();
            }
        });

        //checkState();
        p.add(butDefault);
        p.add(ok);
        p.add(cancel);

        addListeners();

        add(p);
    }

    private void defaultSettings()
    {
        Settings.PLAYER_NAME = "default_name";
        Settings.CONTROLLER_DROP = GameConstants.CONTROLLER_DEFAULT_DROP;
        Settings.CONTROLLER_LEFT = GameConstants.CONTROLLER_DEFAULT_LEFT;
        Settings.CONTROLLER_RIGHT = GameConstants.CONTROLLER_DEFAULT_RIGHT;
        Settings.CONTROLLER_ROTATE = GameConstants.CONTROLLER_DEFAULT_ROTATE;
        Settings.CONTROLLER_SPEEDUP = GameConstants.CONTROLLER_DEFAULT_SPEEDUP;

        int[] controls =
        {
            Settings.CONTROLLER_DROP, Settings.CONTROLLER_LEFT, Settings.CONTROLLER_RIGHT, Settings.CONTROLLER_SPEEDUP, Settings.CONTROLLER_ROTATE
        };

        GameData.save(Settings.PLAYER_NAME, controls);
        dispose();
    }

    /*private void checkState()
    {
        State state = GUIController.getState();

        if (state != State.MENU)
        {
            nameField.setEnabled(false);
            butDefault.setEnabled(false);
        }
    }*/

    private void addListeners()
    {
        //addKeyListener(keylist);

        MouseListener list = new KeyControlSelector();
        dropField.addMouseListener(list);
        leftField.addMouseListener(list);
        rightField.addMouseListener(list);
        speedField.addMouseListener(list);
        rotateField.addMouseListener(list);
    }

    private void ok()
    {
        Settings.PLAYER_NAME = nameField.getText();
        Settings.CONTROLLER_DROP = drop;
        Settings.CONTROLLER_LEFT = left;
        Settings.CONTROLLER_RIGHT = right;
        Settings.CONTROLLER_ROTATE = rotate;
        Settings.CONTROLLER_SPEEDUP = speed;

        int[] controls = new int[]
        {
            drop, left, right, speed, rotate
        };

        GameData.save(nameField.getText(), controls);

        dispose();
    }

    private void cancel()
    {
        dispose();
    }

    private class KeyControlSelector implements MouseListener
    {

        @Override
        public void mouseClicked(MouseEvent e)
        {
            Object o = e.getSource();

            if (o.equals(dropField))
            {
                select = "drop";
            } 
            else if (o.equals(leftField))
            {
                select = "left";
            } 
            else if (o.equals(rightField))
            {
                select = "right";
            } 
            else if (o.equals(speedField))
            {
                select = "speed";
            } 
            else if (o.equals(rotateField))
            {
                select = "rotate";
            }

            if (o.equals(dropField)
                    || o.equals(leftField)
                    || o.equals(rightField)
                    || o.equals(speedField)
                    || o.equals(rotateField))
            {
                selectedControl = (JTextField) o;
                selectedControl.addKeyListener(keylist);
            } else
            {
                selectedControl = null;
            }
        }

        @Override
        public void mouseEntered(MouseEvent e)
        {
        }

        @Override
        public void mouseExited(MouseEvent e)
        {
        }

        @Override
        public void mousePressed(MouseEvent e)
        {
        }

        @Override
        public void mouseReleased(MouseEvent e)
        {
        }
    }

    private class KeySettingsController implements KeyListener
    {

        @Override
        public void keyPressed(KeyEvent e)
        {
            if (selectedControl != null)
            {
                selectedControl.setText(KeyEvent.getKeyText(e.getKeyCode()));
                keyOfSelect = e.getKeyCode();
                
                switch (select)
                {
                    case "drop":
                        drop = keyOfSelect;
                        break;
                    case "left":
                        left = keyOfSelect;
                        break;
                    case "right":
                        right = keyOfSelect;
                        break;
                    case "speed":
                        speed = keyOfSelect;
                        break;
                    case "rotate":
                        rotate = keyOfSelect;
                        break;
                }

            }
            selectedControl.removeKeyListener(keylist);
            selectedControl = null;
        }

        @Override
        public void keyReleased(KeyEvent e)
        {
        }

        @Override
        public void keyTyped(KeyEvent e)
        {
        }
    }
}
